import {reactive, toRefs} from 'vue'
export const useCommon = () => {
  const state = reactive({
    userInfo: null,
    userIsLogin: true,
    VITE_APP_BASE_URL: '',
   
  })
  // 读取.env 多坏境里的数据
  state.VITE_APP_BASE_URL = import.meta.env.VITE_APP_BASE_URL as string
 
  const touchMoveStop = () => {
    return false
  }
  
  const wxShowToastSuccess = (title: string, duration?: number) => {
    sleep(50).then(() => {
      uni.showToast({
        title: title,
        icon: 'success',
        duration: duration || 2000
      })
    })
  }
  const wxShowToastNone = (title: string, duration?: number) => {
    sleep(50).then(() => {
      uni.showToast({
        title: title,
        icon: 'none',
        duration: duration || 2000
      })
    })
  }
  const getWxLoginCode = () => {
    return new Promise((resolve) => {
      uni.login({
        success: (res) => {
          console.log(res)
          resolve(res.code)
        },
        error: (err) => {
          wxShowToastNone('获取code失败')
        }
      })
    })
  }
  const sleep = (time: any) => {
    return new Promise((resolve) => {
      const timer = setTimeout(() => {
        clearTimeout(timer)
        resolve(null)
      }, time)
    })
  }

  return {
    touchMoveStop,
    wxShowToastSuccess,
    wxShowToastNone,
    getWxLoginCode,
    sleep,
    ...toRefs(state)
  }
}

export default useCommon